package v1

import (
    "authserver/constants"
    "authserver/global"
    "authserver/global/response"
    "authserver/model"
    "authserver/service"
    "authserver/utils"
    "encoding/json"
    "github.com/gin-gonic/gin"
)

// TtsAudio
// @Summary 文本转语音
// @Router /api/ttsAudio [post]
func TtsAudio(c *gin.Context)  {

    var param model.TTSAudio
    _ = c.ShouldBindJSON(&param)

    if param.Username == "" {
        global.GVA_LOG.Error("请输入注册码....")
        response.FailWithMessage("请输入注册码", c)
        return
    }
    if param.Text==""{
        response.FailWithMessage("没有需要转换的文本", c)
        return
    }
    if param.ModeType==""{
        response.FailWithMessage("选择音色", c)
        return
    }

    err,shortUrl:=service.TextToAudio(param.Text,param.ModeType,param.Rate,param.Pitch)
    if err!=nil{
        response.FailWithMessage("获取失败", c)
        return
    }
    response.OkWithData(shortUrl,c)
    return
}

// GetVoiceList
// @Summary 获取语音模板列表
// @Router /api/voiceList [post]
func GetVoiceList(c *gin.Context)  {

    options:=make([]map[string]string,0)

    err:=json.Unmarshal([]byte(constants.TTSOptions),&options)
    if err!=nil{
        global.GVA_LOG.Error(err.Error())
        response.FailWithMessage("获取失败", c)
        return
    }

    response.OkWithData(options,c)
}


// TtsAudioSync
// @Summary  异步提交转语音任务
// @Router /api/ttsAudioSync [post]
func TtsAudioSync(c *gin.Context) {


    var param model.TTSAudio
    _ = c.ShouldBindJSON(&param)

    if param.Username == "" {
        global.GVA_LOG.Error("请输入注册码....")
        response.FailWithMessage("请输入注册码", c)
        return
    }
    if param.Text==""{
        response.FailWithMessage("没有需要转换的文本", c)
        return
    }
    if param.ModeType==""{
        response.FailWithMessage("选择音色", c)
        return
    }
    cacheKey := constants.ExportTTSAudio + param.Username

    utils.WriteMapLock(cacheKey,"")

    go func(key string) {
        err,shortUrl:=service.TextToAudio(param.Text,param.ModeType,param.Rate,param.Pitch)
        if err!=nil{
            response.FailWithMessage("获取失败", c)
            return
        }
        if err != nil {
            utils.WriteMapLock(key, "er_"+err.Error())
        } else {
            // 返回完成
            utils.WriteMapLock(key,"ok_"+shortUrl)
        }
    }(cacheKey)
    response.OkWithMessage("提交成功", c)
}

// CheckExportResult
// @Summary 查询异步任务结果
// @Router /api/syncResult [post]
func CheckExportResult(c *gin.Context) {

    actionKey, ok := c.GetQuery("key")
    if !ok {
        response.FailWithMessage("导出任务不存在", c)
        return
    }

    var param model.TTSAudio
    _ = c.ShouldBindJSON(&param)

    if param.Username=="" {
        response.FailWithMessage("导出任务不存在", c)
        return
    }

    // 状态 "" 表示没有结果
    // 查完以后需要删除redis
    cacheKey := actionKey + "_" + param.Username

    result:= utils.GetMapLock(cacheKey)
    if result == "" {
        response.OkWithMessage("load", c)
        return
    }
    if result[0:3] == "ok_" {
        utils.DelMapLock(cacheKey)
        response.OkWithMessage(result[3:], c)
        return
    }
    utils.DelMapLock(cacheKey)
    response.FailWithMessage(result[3:], c)
}
